package com.mxprojects.views
{
	import com.mxprojects.gui.BreadCrumbs;
	import com.mxprojects.gui.CategoryFooter;
	import com.mxprojects.gui.CategoryHeader;
	import com.mxprojects.gui.CategoryItem;
	import com.mxprojects.gui.DefaultDialog;
	import com.mxprojects.gui.ForumItem;
	import com.mxprojects.gui.OnlineUsers;
	import com.mxprojects.gui.Resizer;
	import com.mxprojects.io.BoardCookie;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.CustomEvent;
	import com.mxprojects.io.ServerHandler;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
		
	public class Forums
	{
		public static var forumData:Object; //share with other classes like search
		public static var loadForums:ServerHandler = new ServerHandler();
		
		private static var catHeader:CategoryHeader = new CategoryHeader();
		private static var catFooter:CategoryFooter = new CategoryFooter();
		private static var onlineUsers:OnlineUsers;
		
		public function Forums(){}
		
		public static function load(externalRequest:Boolean = false, externalFunction:Function = null):void {
			
			//if the user is logged in let's send the username along so that we can track threads
			var dataToSend:Object = new Object();
			if(BoardCookie.getUser() != "undefined" && BoardCookie.getUser() != null){
				dataToSend.username = BoardCookie.getUser();
				dataToSend.password = BoardCookie.getPassword();
			}
			//make sure that guests can read stuff
			dataToSend.guestGroupID = Config.GUEST_GROUP_ID;
			dataToSend.maxDaysNewThread = Config.MAX_DAYS_NEW_THREAD;
			dataToSend.lastOnlineTimeout = Config.LAST_ONLINE_TIMEOUT;
			dataToSend.categoryDateFormat = Config.CATEGORY_DATE_FORMAT;
			dataToSend.mostOnlineUsers = Config.MOST_ONLINE_USERS;
			
			loadForums.transmitData("viewcategories.php", dataToSend);
			
			//if asking for a list of forums from another area, just do that
			if(externalRequest){
				dataToSend.bareBones = true;
				loadForums.listenForData(externalFunction);
			}else{
				loadForums.listenForData(showCategories);
				
				//clear the board until it's loaded
				MXP3.clearBoard();
			}
			
		}
		
		private static function showCategories(event:CustomEvent):void {
			//get the category data
			forumData = event.getServerData();
			
			//see if there was some oddness with the login
			if(forumData.output == "badLogin"){
				DefaultDialog.show(Language.main(Word.ERROR_LOGIN_INVALID));
				return;
			}
			
			var nextY:int = 0;
			
			//tell the board where we are
			MXP3.currentLocation = Word.FORUMS;
			
			//put the "home" breadcrumb there
			BreadCrumbs.createCrumbs();
			
			//put the category header up
			catHeader.y = nextY;
			catHeader.resize();
			nextY += catHeader.height - Config.DEFAULT_LINE_HEIGHT;
			MXP3.addContent(catHeader);
			
			//loop through the categories and forums
			for(var i:uint = 0; i < forumData["categoryCount"]; i++){
				var category:CategoryItem = new CategoryItem(forumData["category" + i + "title"]);
				category.y = nextY;
				category.name = "cat" + i;
				MXP3.addContent(category);
				
				//any forums belong to these titles?
				for(var j:uint = 0; j < forumData["forumCount"]; j++){
					if(forumData["category" + i + "ID"] == forumData["forum" + j + "categoryID"]){
						var forum:ForumItem = new ForumItem(forumData["forum" + j + "title"], 
						forumData["forum" + j + "description"], 
						forumData["forum" + j + "postCount"], 
						forumData["forum" + j + "threadCount"],
						forumData["forum" + j + "lastPost"],
						forumData["forum" + j + "lastUsername"],
						catHeader);
						
						var forumY:int = category.height - 1;
						forum.y = forumY;
						forum.name = "forum" + j;
						forumY += forum.height - Config.DEFAULT_LINE_HEIGHT;
						category.addChild(forum);
						
						forum.setProperties(forumData["forum" + j + "forumID"],
						forumData["forum" + j + "lastThreadID"],
						forumData["forum" + j + "forumReadMode"],
						forumData["forum" + j + "newPosts"]);
					}
				}
				
				nextY += category.height - Config.DEFAULT_LINE_HEIGHT;
			}
			
			//put the category footer on
			catFooter.y = nextY;
			catFooter.resize();
			nextY += catFooter.height - Config.DEFAULT_LINE_HEIGHT;
			MXP3.addContent(catFooter);
			
			//let's see who's online
			onlineUsers = new OnlineUsers(forumData["onlineUsers"], forumData["recordUsers"], forumData["recordTime"]);
			onlineUsers.y = nextY;
			MXP3.addContent(onlineUsers);
			
			//resize the page if we need to
			Resizer.resizeContent();
		}
		
		public static function resize():void {
			catHeader.resize();
			catFooter.resize();
			
			//handle the categories
			for(var i:uint = 0; i < forumData["categoryCount"]; i++){
				var category:CategoryItem = MXP3.getContent("cat" + i);
				category.resize();
			}
			
			//take care of the online users
			onlineUsers.resize();
		}
	}
}